<?php

namespace App\Logic;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Resources\Accounting as AccountingResource;
use App\Models\Accounting;
use App\Models\Linkman;
use App\Models\SysUser;
use App\Models\Area;
use PhpParser\Node\Stmt\TryCatch;
use Illuminate\Support\Facades\Hash;
use App\Models\Agent;


class AccountingLogic 
{
	/**
	 * 表单数据
	 * @var array
	 */
	private $_post_data = array();
	
	
	public function __construct()
	{
		
	}
	
	/*
	 * 分页获取企业账单薄列表
	 * return array
	 */
	static public function getAccountingList($where, $page = 1, $limit = 20)
	{
		
		//return AccountingResource::collection(Accounting::where($where)->with('agent')->orderBy('sort', 'desc')->paginate($limit))->additional(['code' =>200,'status' => 'success']);

		$accounting_info = DB::table('cpa_accounting_book as a')
		    ->leftJoin('cpa_customer_info as b','a.customer_info_id', '=', 'b.id')
			->leftJoin('at_enterprise_info as e', 'e.id', '=', 'a.customer_info_id')
			->leftJoin('at_linkman as l', 'e.id', '=', 'l.at_enterprise_info_id')			
			->select('a.id','a.taxpayer_property_id','a.accounting_system_id', 'a.create_date','b.name','b.province','b.city','b.area','e.company_name','e.company_type','l.legal_person')
			->where($where)
			->paginate($limit);
			
       foreach ($accounting_info as $accounting_infos){
       	   $accounting_infos->taxpayer_property_id = Accounting::getLockTaxesDisplayMap()[$accounting_infos->taxpayer_property_id];
       	   $accounting_infos->accounting_system_id = Accounting::getLockAccountDisplayMap()[$accounting_infos->accounting_system_id];
       	   $accounting_infos->company_type = Agent::getAgentStateDisplayMap()[$accounting_infos->company_type];
       	   $accounting_infos->province = AreaLogic::getAreaName($accounting_infos->province);
       	   $accounting_infos->city = AreaLogic::getAreaName($accounting_infos->city);
       	   $accounting_infos->area = AreaLogic::getAreaName($accounting_infos->area);
       }
		
		//dd($accounting_info);
		return $accounting_info;
	}
	
	/**
	 * 获取账薄详情
	 * return array
	 */
   static public function getAccountingDetails($where){
	   	$accounting_details = DB::table('cpa_accounting_book as a')
	   	->leftJoin('cpa_customer_info as b','a.customer_info_id','=','b.id')	   	
	   	->leftJoin('at_enterprise_info as e', 'e.id', '=', 'b.enterprise_marking')	   	
	   	->leftJoin('at_linkman as l', 'e.id', '=', 'l.at_enterprise_info_id')
	   	->select('a.id','a.taxpayer_property_id','a.accounting_system_id', 'a.create_date','a.accounting_end_date','b.uniform_credit_code','b.tax_regist_num','e.company_name','e.company_type','e.address','l.legal_person')
	   	->where($where)
	   	->first();
	   		   	
	   	return $accounting_details;
   }
    
}
